package com.mayikt.thread;

/**
 * @ClassName TicketThread
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class TicketThread implements Runnable {
    // 票数为100张
    private static int count = 100;

    @Override
    public void run() {
        while (count > 0) {
            ticket();
        }
    }

    //
//public static synchronized void ticket() {
//    if (count > 0) {
//        try {
//            Thread.sleep(30);
//        } catch (Exception e) {
//
//        }
//        System.out.println(Thread.currentThread().getName() + ",正在出票第" + (100 - count + 1) + "张");
//        count--;
//    }
//
//}
    private Object object = new Object();

    public void ticket() {
        synchronized (object) {
            if (count > 0) {
                try {
                    Thread.sleep(30);
                } catch (Exception e) {

                }
                System.out.println(Thread.currentThread().getName() + ",正在出票第" + (100 - count + 1) + "张");
                count--;
            }

        }
    }

//    public static  void ticket() {
//        synchronized (TicketThread.class) {
//            if (count > 0) {
//                try {
//                    Thread.sleep(30);
//                } catch (Exception e) {
//
//                }
//                System.out.println(Thread.currentThread().getName() + ",正在出票第" + (100 - count + 1) + "张");
//                count--;
//            }
//        }
//
//    }

    public static void main(String[] args) {
        TicketThread ticketThread = new TicketThread();
        new Thread(ticketThread, "窗口1").start();
        new Thread(ticketThread, "窗口2").start();
    }
}
